package com.hs.bai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.bai.service.IGoodsinfoService;
import com.hs.bai.pojo.Goodsinfo;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 货物信息 前端控制器
 * </p>
 *
 * @author Dingkun Bai
 * @since 2022-06-13
 */
@CrossOrigin
@RestController
@RequestMapping("/goodsinfo")
@Api(tags = "订单货物信息模块")
public class GoodsinfoController {

    @Autowired
    private IGoodsinfoService goodsinfoService;

    /**
     * 添加订单货物信息
     * @param list
     * @return
     */
    @PostMapping("/add/{orderId}")
    @ApiOperation("添加订单货物信息")
    public ResultBean addBasicOrderInfo(@PathVariable String orderId ,@RequestBody List<Goodsinfo> list){

        System.out.println("list = " + list);
        System.out.println("orderId = " + orderId);

        //设置订单编号
        list.forEach(e -> {

            e.setOrderId(orderId);

        });

        if (!CollectionUtils.isEmpty(list)) {

            boolean b = goodsinfoService.saveBatch(list);
            return b ? ResultBean.OK("添加成功！"):ResultBean.ERROR("添加失败！");

        }
        return ResultBean.ERROR("添加失败！");

    }

    /**
     * 修改订单货物信息
     * @param list
     * @return
     */
    @PostMapping("/update/{orderId}")
    @ApiOperation("添加订单货物信息")
    public ResultBean updateBasicOrderInfo(@PathVariable String orderId ,@RequestBody List<Goodsinfo> list){

        //设置订单编号
        list.forEach(e -> {
            e.setOrderId(orderId);
        });

        if (!CollectionUtils.isEmpty(list)) {
            boolean b = goodsinfoService.saveOrUpdateBatch(list);
            return b ? ResultBean.OK("修改成功！"):ResultBean.ERROR("修改失败！");
        }
        return ResultBean.ERROR("修改失败！");

    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/find/{id}")
    public ResultBean findById(@PathVariable String id){
        QueryWrapper<Goodsinfo> qw = new QueryWrapper<>();
        qw.eq("order_id",id);
        List<Goodsinfo> goodsinfos = goodsinfoService.list(qw);
        ResultBean ok = ResultBean.OK();
        ok.setData(goodsinfos);
        return ok;
    }


    @GetMapping("/page")
    public ResultBean findAllByPage(@RequestParam String orderId, @RequestParam Integer currentPage, @RequestParam Integer pageSize){

        IPage<Goodsinfo> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Goodsinfo> qw = new QueryWrapper<>();
        //设置订单状态
        qw.eq("order_id",orderId);

        //查询
        IPage<Goodsinfo> goodsinfoIPage = goodsinfoService.page(page, qw);

        PageBean<Goodsinfo> pageBean = new PageBean<>();
        if (goodsinfoIPage != null) {

            pageBean.setList(goodsinfoIPage.getRecords());
            pageBean.setTotal((int) goodsinfoIPage.getTotal());
            val resultBean = ResultBean.OK();
            resultBean.setData(pageBean);
            return resultBean;

        }
        return ResultBean.ERROR();

    }

}
